package com.spongebob.controller.ly;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spongebob.pojo.VisitorInfo;
import com.spongebob.service.CustomerServiceService;
import com.spongebob.service.VisitorInfoService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 视图统计 前端控制器
 * </p>
 *
 * @author tutu
 * @since 2021-03-16
 */

/**
 * 进入->访客界面
 * @return
 */
@Controller

public class VisitorInfoController {

    @Autowired
    private VisitorInfoService visitorInfoService;

    @Autowired
    CustomerServiceService customerServiceService;

    /**
     * 进入访客页面
     * @param session
     * @return
     */
    @RequestMapping("/visitor")
    public String Visitors(HttpSession session,Model model){

        System.out.println("访客页VisitorInfoController进入访客页面");
        List<VisitorInfo> visitorInfoList= visitorInfoService.list();
        System.out.println(visitorInfoList);
        model.addAttribute("vlists",visitorInfoList);
        model.addAttribute("status",4);
        return "index";

    }

    /**
     * 导出数据
     * @param session
     * @param response
     * @throws IOException
     * @throws ParseException
     */
    @RequestMapping(value = "/vsdownload",method = RequestMethod.GET)
    public void visdonload(HttpSession session, HttpServletResponse response) throws IOException, ParseException {
        System.out.println("进入下载");
        List<VisitorInfo> visitorInfoList = visitorInfoService.list();
        System.out.printf("------------" + visitorInfoList.toString());
        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFSheet sheet = wb.createSheet("获取excel测试表格");

        HSSFRow row = null;

        row = sheet.createRow(0);//创建第一个单元格
        row.setHeight((short) (26.25 * 20));
        row.createCell(0).setCellValue("访客信息列表");//为第一行单元格设值

        /*为标题设计空间
         * firstRow从第1行开始
         * lastRow从第0行结束
         *
         *从第1个单元格开始
         * 从第3个单元格结束
         */
        CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
        sheet.addMergedRegion(rowRegion);
        sheet.setColumnWidth(0, 3000);
      /*CellRangeAddress columnRegion = new CellRangeAddress(1,4,0,0);
      sheet.addMergedRegion(columnRegion);*/

        row = sheet.createRow(1);
        row.setHeight((short) (22.50 * 20));//设置行高
        row.createCell(0).setCellValue("访客姓名");//为第一个单元格设值
        row.createCell(1).setCellValue("ip地址");//为第二个单元格设值
        row.createCell(2).setCellValue("终端");//为第三个单元格设值
        row.createCell(3).setCellValue("设备");//为第四个单元格设值
        row.createCell(4).setCellValue("访问时间");
        row.createCell(5).setCellValue("状态");

        //遍历所获取的数据
        for (int i = 0; i < visitorInfoList.size(); i++) {
            row = sheet.createRow(i + 2);
            VisitorInfo visitorInfo = visitorInfoList.get(i);
            row.createCell(0).setCellValue(visitorInfo.getVisitorname());
            row.createCell(1).setCellValue(visitorInfo.getIp());
            row.createCell(2).setCellValue(visitorInfo.getTerminal());
            row.createCell(3).setCellValue(visitorInfo.getDevice());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String vtime= sdf.format(visitorInfo.getVisittime());

            row.createCell(4).setCellValue(visitorInfo.getVisittime());
            row.createCell(5).setCellValue(visitorInfo.getState());
        }
        sheet.setDefaultRowHeight((short) (16.5 * 20));
        //列宽自适应
        for (int i = 0; i <= 13; i++) {
            sheet.autoSizeColumn(i);
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename=visitorinfo.xls");//默认Excel名称
        wb.write(os);
        os.flush();
        os.close();

    }

    /**
     * 访问时间排序
     * @param model
     * @param session
     * @return
     */

    @RequestMapping("/vstime")
    public String vstime(Model model,HttpSession session){

        QueryWrapper qw = new QueryWrapper();
        qw.orderByDesc("visittime");

        List<VisitorInfo> timeList=visitorInfoService.list(qw);
        model.addAttribute("status",4);
        model.addAttribute("vlists",timeList);

        return "index";
    }
    /**
     * 只显示在线
     * @param model
     *
     * @return
     */
    @RequestMapping("/onlie")
    public String onlie(Model model){

        QueryWrapper qw = new QueryWrapper();
        qw.eq("state","在线");
        List<VisitorInfo> onlieList = visitorInfoService.list(qw);
        model.addAttribute("vlists",onlieList);
        model.addAttribute("status",4);

        return "index";
    }


}
